<template>
  <a-card :bordered="false">
    <!-- 查询区域 -->
    <search ref="search"  :config="searchConfig" @search="search" @getHeight="getHeight"/>

    <!-- table区域-begin -->
    <div class='page_table'>
      <vxe-grid
        ref='xGrid'
        v-bind='vexConfig'
        :align='left'
        :border='true'
        :checkbox-config='checkboxConfig'
        :columns='columns'
        :data='dataSource'
        :height='pageHeight-seachHeight-94'
        :loading='loading'
        :resizable='true'
        :showOverflow='true'
        :toolbar-config='tableToolbar'
        size='mini'
        @checkbox-change='selectChangeEvent'
        @checkbox-all='selectChangeEvent'
      >
        <template #toolbar_buttons>
          <!-- 操作按钮区域 -->
          <div class='table-operator'>
            <a-button type="primary" @click="handleAdd">新增</a-button>
            <a-button type="primary" @click="startorstop(1)">启用</a-button>
            <a-button type="primary" @click="startorstop(0)">停用</a-button>
          </div>
        </template>
        <template #materialCode='{ row,rowIndex }'>
          <a @click='handleDetail(row)'>{{ row.materialCode }}</a>
        </template>
        <template #status='{ row }'>
          <a-switch :checked="row.status" size="small" @change="handleStatus(row,$event)"/>
        </template>
        <template #action='{ row:record }'>
          <a @click="handleEdit(record)">编辑</a>
          <a-divider type="vertical" />
          <a-dropdown>
            <a class="ant-dropdown-link">更多 <a-icon type="down" /></a>
            <a-menu slot="overlay">
              <a-menu-item>
                <a-popconfirm title="确定删除吗?" @confirm="() => handleDelete(record.id)">
                  <a>删除</a>
                </a-popconfirm>
              </a-menu-item>
            </a-menu>
          </a-dropdown>
        </template>

        <template #pager>
          <vxe-pager
            :currentPage.sync='ipagination.current'
            :layouts="['Total', 'PrevJump', 'PrevPage', 'Number', 'NextPage', 'NextJump','Sizes', 'FullJump']"
            :page-size.sync='ipagination.pageSize'
            :pageSizes='ipagination.pageSizeOptions'
            :total='ipagination.total'
            @page-change='handlePageChange'>
          </vxe-pager>
        </template>
      </vxe-grid>
    </div>
    <!-- table区域-end -->

    <!-- 表单区域 -->
    <priceAdd ref="modalForm" @ok="modalFormOk"></priceAdd>
  </a-card>
</template>

<script>
import JSuperQuery from '@/components/cn-form/JSuperQuery.vue'
import JInput from '@/components/cn-form/JInput.vue'
import { initDictOptions, filterDictText, filterDictTextByCache } from '@/components/dict/CnDictSelectUtil'
import { PageListMixin } from '@/mixins/PageListMixin'
import Vue from 'vue'
import { filterObj } from '@/utils/util'
import JTreeSelect from '@/components/cn-form/JTreeSelect'
import { postAction } from '../../../api/manage'
import priceAdd from './modules/priceAdd'
import { getAction } from '@/api/manage'
//高级查询modal需要参数

export default {
  name: 'priceManager',
  mixins: [PageListMixin],
  components: {
    JSuperQuery,
    JInput,
    JTreeSelect,
    priceAdd,
  },
  data() {
    return {
      description: '价格维护',
      //字典数组缓存
      sexDictOptions: [],
      importExcelUrl: `${window._CONFIG['domianURL']}/test/cn-formDemo/importExcel`,
      searchConfig:[
        {
          type:'input',
          name:'materialCode',
          placeholder:'请输入物料编号',
        },
        {
          type:'input',
          name:'materialName',
          placeholder:'请输入物料名称',
        }, {
          type:'input',
          name:'custCode',
          placeholder:'请输入经销商编号',
        }, {
          type:'input',
          name:'custName',
          placeholder:'请输入经销商名称',
        }],
      //表头
      columns: [],
      //列设置
      settingColumns: [],
      //列定义  表格改这儿
      defColumns: [
        { type: 'checkbox', width: 50, fixed: null ,field: 'a',},
        {
          title: '物料编号',
          minWidth: 80,
          field: 'materialCode',
          // slots: { default: 'materialCode' },
        },
        {
          title: '物料名称',
          minWidth: 80,
          field: 'materialName',
        },
        {
          title: '经销商编号',
          minWidth: 80,
          field: 'custCode'
        },
        {
          title: '经销商名称',
          minWidth: 80,
          field: 'custName'
        },
        {
          title: '价格组',
          minWidth: 80,
          width: 60,
          field: 'priceGroup'
        },
        {
          title: '部门定价',
          minWidth: 80,
          field: 'departName'
        },
        {
          title: '价格',
          minWidth: 80,
          field: 'price'
        },
        {
          title: '价格数量',
          minWidth: 80,
          field: 'kpein'
        },
        {
          title: '定价单位名',
          minWidth: 100,
          field: 'priceUnitName'
        },
        {
          title: '开始时间',
          minWidth: 120,
          field: 'startTime'
        },
        {
          title: '结束时间',
          minWidth: 120,
          field: 'endTime'
        },
        {
          title: '当前单位数量',
          minWidth: 120,
          field: 'umren'
        },
        {
          title: '单位系数',
          minWidth: 80,
          field: 'umrez'
        },

        {
          title: 'kappl',
          minWidth: 80,
          field: 'kappl'
        },
        {
          title: '定价条件',
          minWidth: 80,
          field: 'kschl'
        },
        {
          title: '组织',
          minWidth: 80,
          field: 'vkorg'
        },
        {
          title: '渠道',
          minWidth: 80,
          field: 'vtweg'
        },
        {
          title: '币种',
          minWidth: 80,
          field: 'konwa'
        },
        {
          title: '创建时间',
          minWidth: 120,
          field: 'createTime',
        },
        {
          title: '最近更新人',
          minWidth: 100,
          field: 'updateName',
          ellipsis: true
        },
        {
          title: '最近更新时间',
          minWidth: 120,
          field: 'updateTime'
        },
        {
          title: '状态',
          minWidth: 80,
          field: 'status',
          slots: { default: 'status' },
        },
        {
          title: '操作',
          field: 'action',
          fixed: 'right',
          minWidth: 150,
          slots: { default: 'action' },
        }
      ],
      url: {
        list: window._CONFIG['domianURL'] + '/price/findPageList',
        detail: window._CONFIG['domianURL'] + '/price/getInfo',
        delete: window._CONFIG['domianURL'] + '/price/deleteById',
        startOrStopUrl: window._CONFIG['domianURL'] + '/price/startOrStop',
        deleteBatch: '/test/cn-formDemo/deleteBatch',
        exportXlsUrl: '/test/cn-formDemo/exportXls'

      }

    }
  },
  methods: {
    startorstop: function(status) {
      if (!this.url.startOrStopUrl) {
        this.$message.error('请设置url启用停用!')
        return
      }
      if (this.selectedRowKeys.length <= 0) {
        this.$message.warning('请选择一条记录！')
      } else {
        var ids = ''
        for (var a = 0; a < this.selectedRowKeys.length; a++) {
          ids += this.selectedRowKeys[a] + ','
        }
        var that = this
        this.$confirm({
          title: '确认修改',
          content: '是否修改选中数据?',
          onOk: function() {
            that.loading = true
            getAction(that.url.startOrStopUrl, { ids: ids, status: status }).then((res) => {
              if (res.success) {
                that.$message.success(res.message)
                that.loadData()
                that.onClearSelected()
              } else {
                that.$message.warning(res.message)
              }
            }).finally(() => {
              that.loading = false
            })
          }
        })
      }
    },
    selectCusttype(value) {
      this.$set(this.queryParam, 'custType', value)
    },

    getQueryParams() {
      //高级查询器
      let sqp = {}
      if (this.superQueryParams) {
        sqp['superQueryParams'] = encodeURI(this.superQueryParams)
        sqp['superQueryMatchType'] = this.superQueryMatchType
      }
      var param = Object.assign(sqp, this.queryParam, this.isorter, this.filters)

      param.field = this.getQueryField()
      param.current = this.ipagination.current
      param.size = this.ipagination.pageSize
      delete param.birthdayRange //范围参数不传递后台
      return filterObj(param)
    },
    // initDictConfig() {
    //   console.log("--我才是真的方法!--")
    //初始化字典 - 性别
    // initDictOptions('sex').then((res) => {
    //   if (res.success) {
    //     this.sexDictOptions = res.result;
    //   }
    // });
    // },
    //跳转单据页面
    jump() {
      this.$notification.success('跳转 ： jump')
    },
    onBirthdayChange: function(value, dateString) {
      console.log(dateString[0], dateString[1])
      this.queryParam.birthday_begin = dateString[0]
      this.queryParam.birthday_end = dateString[1]
    },
    //列设置更改事件
    onColSettingsChange(checkedValues) {
      var key = this.$route.name + ':colsettings'
      Vue.ls.set(key, checkedValues, 7 * 24 * 60 * 60 * 1000)
      this.settingColumns = checkedValues
      const cols = this.defColumns.filter(item => {
        if (item.key == 'rowIndex' || item.field == 'action') {
          return true
        }
        if (this.settingColumns.includes(item.field)) {
          return true
        }
        return false
      })
      this.columns = cols
    },
    initColumns() {
      //权限过滤（列权限控制时打开，修改第二个参数为授权码前缀）
      //this.defColumns = colAuthFilter(this.defColumns,'testdemo:');

      var key = this.$route.name + ':colsettings'
      let colSettings = Vue.ls.get(key)
      if (colSettings == null || colSettings == undefined) {
        let allSettingColumns = []
        this.defColumns.forEach(function(item, i, array) {
          allSettingColumns.push(item.field)
        })
        this.settingColumns = allSettingColumns
        this.columns = this.defColumns
      } else {
        this.settingColumns = colSettings
        const cols = this.defColumns.filter(item => {
          if (item.key == 'rowIndex' || item.field == 'action') {
            return true
          }
          if (colSettings.includes(item.field)) {
            return true
          }
          return false
        })
        this.columns = cols
      }
    }
  },
  created() {
    this.initColumns()
  }

}
</script>
<style scoped>
@import '~@assets/less/common.less';
</style>